export default {
    // 身份证解析
    IdcardParse(IdCard, type) {
        if (type === 'birthday') {
            //获取出生日期
            var birthday = IdCard.substring(6, 10) + "-" + IdCard.substring(10, 12) + "-" + IdCard.substring(12, 14)
            return birthday
        }
        if (type === 'sex') {
            //获取性别
            var sex = parseInt(IdCard.substring(16, 17)) % 2 == 1 ? '1' : '2'
            return sex
        }
        if (type === 'age') {
            //获取年龄
            var ageDate = new Date()
            var month = ageDate.getMonth() + 1
            var day = ageDate.getDate()
            var age = ageDate.getFullYear() - IdCard.substring(6, 10) - 1
            if (IdCard.substring(10, 12) < month || IdCard.substring(10, 12) == month && IdCard.substring(12, 14) <= day) {
                age++
            }
            if (age <= 0) {
                age = 1
            }
            return age
        }
    },

    // 根据生日取得年龄
    BirthdayAge(val) {
        //获取年龄
        var ageDate = new Date()
        var month = ageDate.getMonth() + 1
        var day = ageDate.getDate()
        var age = ageDate.getFullYear() - val.substring(0, 4) - 1
        if (val.substring(5, 7) < month || val.substring(5, 7) == month && val.substring(8, 10) <= day) {
            age++
        }
        if (age <= 0) {
            age = 1
        }
        return age
    },

    // 时间转时间戳
    DateTimestamp(val) {
        var time;
        if (val != "") {
            time = new Date(val).getTime();
        }
        return time;
    },

    // 时间戳转时间
    timestampToTime(value) {
        let date = new Date(value);
        let y = date.getFullYear(); // 年
        let MM = date.getMonth() + 1; // 月
        MM = MM < 10 ? "0" + MM : MM;
        let d = date.getDate(); // 日
        d = d < 10 ? "0" + d : d;
        let h = date.getHours(); // 时
        h = h < 10 ? "0" + h : h;
        let m = date.getMinutes(); // 分
        m = m < 10 ? "0" + m : m;
        let s = date.getSeconds(); // 秒
        s = s < 10 ? "0" + s : s;
        return y + "-" + MM + "-" + d + " " + h + ":" + m + ":" + s;
    },
}